<?php

namespace app\api\controller\summary;

use app\api\controller\Base;
use app\api\controller\Pub;
use app\common\model\coll\Collection;
use app\common\model\coll\Collection as Coll;
use app\common\model\company\Company;
use app\common\model\cost\Costjournal;
use app\common\model\cost\Costjournalinfo;
use app\common\model\cost\Costtype;
use app\common\model\cost\Pay;
use app\common\model\cost\Revenue;
use app\common\model\power\Rolepower;
use app\common\model\user\User as UserModel;

class Cost extends Base
{
	/**
	 * 产生jwt加密串，参考session
	 */
	public function initialize()
	{
		parent::initialize();
		self::is_login();
		Rolepower::powerguard(203008);
	}


	/**
	 * 获取订单信息
	 * @return [type] [description]
	 */
	public function getinfo()
	{
		$date = input('date');
		$now = date('Y-m', strtotime($date)) . '-01';
		$end = date('Y-m', strtotime($date)) . '-32';
		if (empty($now) || empty($end)) {
			return myerr('缺少参数，请重新尝试');
		}
		$timestr = $now . '~' . $end;
		//通用 条件
		$ndata['where'] = [['PayTime', '>', $now], ['PayTime', '<', $end]];
		//通用财务分类
		$sb = Costtype::getlist('costview');
		foreach ($sb as $key => $value) {
			$rdata['type'][$value['TypeId']] = $value['TypeName'];
		}
		//支出
		$rdata['pay'] = Pay::usergetlist($ndata, 'summary', false);
		//收入
		$rdata['revenue'] = Revenue::usergetlist($ndata, 'summary', false);
		//通用 条件
		$ndata['where'] = [['UpDate', '>=', $now], ['UpDate', '<=', $end]];
		$ndata['order'] = 'UpDate desc';
		//汇总
		$journal = Costjournal::usergetlist($ndata, 'summary', false);
		$journalinfo = Costjournalinfo::usergetlist($ndata, 'summary', false);

		foreach ($journal['list'] as $key => $value) {
			$rdata['journal'][$value['id']] = $value;
		}
		foreach ($journalinfo['list'] as $key => $value) {
			$rdata['journal'][$value['cid']]['list'][] = $value;
		}

		return myok($rdata);
	}


	/**
	 * 判断账号是否登录
	 * @return boolean [description]
	 */
	private function is_login()
	{
		$data = UserModel::is_sessionlogin();
		if (!$data['code']) {
			$data['options'] = 'relogin';
			json($data)->send();
			exit;
		}
	}
	
}
